var pages = 1;
var loading = false; //状态标记
$(function() {
    loadlist();
})

var vm = new Vue({
	el: "#app",
	data: {
		//标记筛选头
		checkCity: false,
		checkHouse: false,
		checkArea: false,
		checkSort: false,
		//标记筛选条件
		checkCityItemIndex:0,
		checkHouseItemIndex:0,
		checkAreaItemIndex:0,
		checkSortItemIndex:0,
		//筛选头
		city: "城市",
		house: "户型",
		area: "面积",
		sort: "排序",
		//数据
		projectList:[],
		//条件
		cityList:[],
		houseList:[],
		areaList:[],
		sortList:[],
		//条件值
		cityItem:"不限",
		houseItem:"不限",
		areaItem:"不限",
		sortItem:"默认排序",

	},
    filters:{
        //过滤器
        setCount:function (value) {

        	   if(value.length>3){
                   return  value.slice(0,3)+"...";
			   }
			   else{
                   return  value;
			   }

        }
    },
    mounted:function () {
        this.$nextTick(function () {
            vm.listView();
        });

    },
	methods: {
		//获取条件
        listView:function () {
            var _this=this;
            this.$http.get("lists",{"flag":1}).then(function (res) {
            	//console.log(res.data);
                _this.cityList=res.data;

            });
            this.$http.get("lists",{"flag":2}).then(function (res) {
               // console.log(res.data);
                _this.houseList=res.data;

            });
            this.$http.get("lists",{"flag":3}).then(function (res) {
                //console.log(res.data);
                _this.areaList=res.data;

            });

        },
		//筛选头选中
		checkSelect: function(n) {

			if(n == 1) {
				this.checkCity = !this.checkCity;
				this.checkHouse = false;
				this.checkArea = false;
				this.checkSort = false;

			}
			if(n == 2) {
				this.checkHouse = !this.checkHouse;
				this.checkCity = false;
				this.checkArea = false;
				this.checkSort = false;

			}
			if(n == 3) {
				this.checkArea = !this.checkArea;
				this.checkCity = false;
				this.checkHouse = false;
				this.checkSort = false;

			}


		},

		//条件选中
		itemCheck: function(n,item,index) {
			if(n == 1) {

				this.city =item.name;
				this.checkCityItemIndex=index;
				this.checkCity = false;
				this.cityItem=item.name;
				console.log(this.cityItem);
				pages=1;
				loadlist();


			}
			if(n == 2) {
				this.house = item.name;
                this.checkHouseItemIndex=index;
                this.checkHouse = false;
                this.houseItem=item.name;
                console.log(this.houseItem);
                pages=1;
                loadlist();

			}
			if(n == 3) {
				this.area =item.name;
                this.checkAreaItemIndex=index;
                this.checkArea = false;
                this.areaItem=item.name;
                console.log(this.areaItem);
                pages=1;
                loadlist();

			}


		},

		//点击遮盖
		checkOver: function() {
			this.checkCity = false;
			this.checkHouse = false;
			this.checkArea = false;
			this.checkSort = false;
		},
		projectCheck:function (item) {
			var s="detail/id/"+item.id;
			location.href=s;
        }
	}
});


//=========================下拉刷新
$("#listwrap").pullToRefresh().on("pull-to-refresh", function() {
	setTimeout(function() {
		pages = 1;
		loadlist();
		if(loading) loading = false;
		$("#listwrap").pullToRefreshDone(); // 重置下拉刷新
	}, 1000); //模拟延迟
});
//============================滚动加载
$("#listwrap").infinite(50).on("infinite", function() {

	if(loading) return;
	loading = true;
	pages++; //页数
    console.log("加载"+pages);
	$('.weui-loadmore').show();
	setTimeout(function() {
		loadlist();
		loading = false;
	}, 1000); //模拟延迟
});
var i=0;
function loadlist() {
    console.log("刷新方法"+i);
    console.log("页数"+pages);
    i++;
    var str="";
    $.ajax({
        type:"post",
        url:"cases",
        dataType:'JSON',
        data:{city:vm.cityItem,house:vm.houseItem,area:vm.areaItem,page:pages},
        error: function (){
            $(".weui-loadmore").hide();
            str += " <div class=\"weui-cells__title\" style=\"text-align: center\">\n" +
                "                   <div class=\"live live-kulian\" style=\"font-size: 100px\"></div>\n" +
                "                    <div style=\"font-size: 30px\">服务器出错了！</div>\n" +
                "                </div>\n";
            $("#list").html(str);
        },
        success: function (data) {

        	console.log(data);
            if (data.length > 0){
                if(pages==1){
                    vm.projectList=data;
                }
                else{
                    vm.projectList=vm.projectList.concat(data);
                }
                $("#listend").html("");
            }
            else {
            	if(pages==1){
            		vm.projectList=[];
				}
                str += "<div class=\"weui-cells__title\" style=\"text-align: center\">\n" +
                    "                    无更多数据\n" +
                    "                </div>";
                $("#listend").html(str);
                loading = true;
            }
            $(".weui-loadmore").hide();
        }
    });

}
